package org.smile.beans.property;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
/**
 * 不区分大小写的 key 与 property 转换
 * @author strive
 *
 */
public class IgnoreCasePropertyConverter implements PropertyConverter {

	private PropertyDescriptor[] properties;
	/**
	 * 构造涵数
	 * @param c
	 * @throws IntrospectionException
	 */
	public IgnoreCasePropertyConverter(Class c) throws IntrospectionException{
		properties= Introspector.getBeanInfo(c).getPropertyDescriptors();
	}
	/**
	 * 把map的键转成 bean的 属性
	 */
	public PropertyDescriptor keyToProperty(String name) {
		for(int i=0;i<properties.length;i++){
			String propertyName=properties[i].getName();
			if(propertyName.equalsIgnoreCase(name)){
				return properties[i];
			}
		}
		return null;
	}
	/**
	 * 把bean的属性转成 map的key
	 */
	public String propertyToKey(PropertyDescriptor property) {
		String propertyName=property.getName();
		return propertyName.toLowerCase();
	}
}
